{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 先导包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "#先导包\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from efficient_apriori import apriori"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据读取分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>10</th>\n",
       "      <th>11</th>\n",
       "      <th>12</th>\n",
       "      <th>13</th>\n",
       "      <th>14</th>\n",
       "      <th>15</th>\n",
       "      <th>16</th>\n",
       "      <th>17</th>\n",
       "      <th>18</th>\n",
       "      <th>19</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>shrimp</td>\n",
       "      <td>almonds</td>\n",
       "      <td>avocado</td>\n",
       "      <td>vegetables mix</td>\n",
       "      <td>green grapes</td>\n",
       "      <td>whole weat flour</td>\n",
       "      <td>yams</td>\n",
       "      <td>cottage cheese</td>\n",
       "      <td>energy drink</td>\n",
       "      <td>tomato juice</td>\n",
       "      <td>low fat yogurt</td>\n",
       "      <td>green tea</td>\n",
       "      <td>honey</td>\n",
       "      <td>salad</td>\n",
       "      <td>mineral water</td>\n",
       "      <td>salmon</td>\n",
       "      <td>antioxydant juice</td>\n",
       "      <td>frozen smoothie</td>\n",
       "      <td>spinach</td>\n",
       "      <td>olive oil</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>burgers</td>\n",
       "      <td>meatballs</td>\n",
       "      <td>eggs</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>chutney</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>turkey</td>\n",
       "      <td>avocado</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>mineral water</td>\n",
       "      <td>milk</td>\n",
       "      <td>energy bar</td>\n",
       "      <td>whole wheat rice</td>\n",
       "      <td>green tea</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7496</th>\n",
       "      <td>butter</td>\n",
       "      <td>light mayo</td>\n",
       "      <td>fresh bread</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7497</th>\n",
       "      <td>burgers</td>\n",
       "      <td>frozen vegetables</td>\n",
       "      <td>eggs</td>\n",
       "      <td>french fries</td>\n",
       "      <td>magazines</td>\n",
       "      <td>green tea</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7498</th>\n",
       "      <td>chicken</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7499</th>\n",
       "      <td>escalope</td>\n",
       "      <td>green tea</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7500</th>\n",
       "      <td>eggs</td>\n",
       "      <td>frozen smoothie</td>\n",
       "      <td>yogurt cake</td>\n",
       "      <td>low fat yogurt</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7501 rows × 20 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                 0                  1            2                 3   \\\n",
       "0            shrimp            almonds      avocado    vegetables mix   \n",
       "1           burgers          meatballs         eggs               NaN   \n",
       "2           chutney                NaN          NaN               NaN   \n",
       "3            turkey            avocado          NaN               NaN   \n",
       "4     mineral water               milk   energy bar  whole wheat rice   \n",
       "...             ...                ...          ...               ...   \n",
       "7496         butter         light mayo  fresh bread               NaN   \n",
       "7497        burgers  frozen vegetables         eggs      french fries   \n",
       "7498        chicken                NaN          NaN               NaN   \n",
       "7499       escalope          green tea          NaN               NaN   \n",
       "7500           eggs    frozen smoothie  yogurt cake    low fat yogurt   \n",
       "\n",
       "                4                 5     6               7             8   \\\n",
       "0     green grapes  whole weat flour  yams  cottage cheese  energy drink   \n",
       "1              NaN               NaN   NaN             NaN           NaN   \n",
       "2              NaN               NaN   NaN             NaN           NaN   \n",
       "3              NaN               NaN   NaN             NaN           NaN   \n",
       "4        green tea               NaN   NaN             NaN           NaN   \n",
       "...            ...               ...   ...             ...           ...   \n",
       "7496           NaN               NaN   NaN             NaN           NaN   \n",
       "7497     magazines         green tea   NaN             NaN           NaN   \n",
       "7498           NaN               NaN   NaN             NaN           NaN   \n",
       "7499           NaN               NaN   NaN             NaN           NaN   \n",
       "7500           NaN               NaN   NaN             NaN           NaN   \n",
       "\n",
       "                9               10         11     12     13             14  \\\n",
       "0     tomato juice  low fat yogurt  green tea  honey  salad  mineral water   \n",
       "1              NaN             NaN        NaN    NaN    NaN            NaN   \n",
       "2              NaN             NaN        NaN    NaN    NaN            NaN   \n",
       "3              NaN             NaN        NaN    NaN    NaN            NaN   \n",
       "4              NaN             NaN        NaN    NaN    NaN            NaN   \n",
       "...            ...             ...        ...    ...    ...            ...   \n",
       "7496           NaN             NaN        NaN    NaN    NaN            NaN   \n",
       "7497           NaN             NaN        NaN    NaN    NaN            NaN   \n",
       "7498           NaN             NaN        NaN    NaN    NaN            NaN   \n",
       "7499           NaN             NaN        NaN    NaN    NaN            NaN   \n",
       "7500           NaN             NaN        NaN    NaN    NaN            NaN   \n",
       "\n",
       "          15                 16               17       18         19  \n",
       "0     salmon  antioxydant juice  frozen smoothie  spinach  olive oil  \n",
       "1        NaN                NaN              NaN      NaN        NaN  \n",
       "2        NaN                NaN              NaN      NaN        NaN  \n",
       "3        NaN                NaN              NaN      NaN        NaN  \n",
       "4        NaN                NaN              NaN      NaN        NaN  \n",
       "...      ...                ...              ...      ...        ...  \n",
       "7496     NaN                NaN              NaN      NaN        NaN  \n",
       "7497     NaN                NaN              NaN      NaN        NaN  \n",
       "7498     NaN                NaN              NaN      NaN        NaN  \n",
       "7499     NaN                NaN              NaN      NaN        NaN  \n",
       "7500     NaN                NaN              NaN      NaN        NaN  \n",
       "\n",
       "[7501 rows x 20 columns]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#先把数据读取进来\n",
    "dataset=pd.read_csv(\"Market_Basket_Optimisation.csv\",header=None)#header=None表示这个数据表没有表头，设置为空,否则会把第一行的数据当成表头\n",
    "dataset#可以看到数据有很多空值，一共有20列 表示一个人最多买东西的时候 买了20种东西"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 通过表中购买的商品名称 来构建transactions  准备构建好了放到算法里面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['shrimp', 'almonds', 'avocado', 'vegetables mix', 'green grapes', 'whole weat flour', 'yams', 'cottage cheese', 'energy drink', 'tomato juice', 'low fat yogurt', 'green tea', 'honey', 'salad', 'mineral water', 'salmon', 'antioxydant juice', 'frozen smoothie', 'spinach', 'olive oil']\n",
      "['burgers', 'meatballs', 'eggs', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan']\n",
      "['chutney', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan']\n",
      "['turkey', 'avocado', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan']\n",
      "['mineral water', 'milk', 'energy bar', 'whole wheat rice', 'green tea', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan']\n",
      "['low fat yogurt', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan']\n"
     ]
    }
   ],
   "source": [
    "#想办法把数据存到 trasactions中\n",
    "transactions=[]\n",
    "#遍历一下dataframe中的数据 \n",
    "for i in range(0,dataset.shape[0]):#遍历所有的行\n",
    "    temp=[]\n",
    "    for j in range(0,dataset.shape[1]): #在每一行中去遍历每一个列元素，dataset.shape[0]=7501 dataset.shape[1]=20\n",
    "        #把每一个元素转成一个字符串str 并添加到temp列表中\n",
    "        temp.append(str(dataset.values[i,j]))#这里range的范围分别取到7500和19 而不是7501和20 表格中最大的行列索引也刚好是7500与19\n",
    "    #这个temp会保存每一行的购物篮的商品的名称的str类型 不过进行下一个for循环会被重新赋值为0 重新存\n",
    "    if i<6:\n",
    "        print(temp)    #跟预期的效果 是一样的 不过有太多的空值   解决方法见下一个cell里面的代码\n",
    "        #print前5行吧 否则上传到github之后 全部显示出来 导致网页太长 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['shrimp', 'almonds', 'avocado', 'vegetables mix', 'green grapes', 'whole weat flour', 'yams', 'cottage cheese', 'energy drink', 'tomato juice', 'low fat yogurt', 'green tea', 'honey', 'salad', 'mineral water', 'salmon', 'antioxydant juice', 'frozen smoothie', 'spinach', 'olive oil'], ['burgers', 'meatballs', 'eggs'], ['chutney'], ['turkey', 'avocado'], ['mineral water', 'milk', 'energy bar', 'whole wheat rice', 'green tea'], ['low fat yogurt'], ['whole wheat pasta', 'french fries'], ['soup', 'light cream', 'shallot'], ['frozen vegetables', 'spaghetti', 'green tea'], ['french fries']]\n"
     ]
    }
   ],
   "source": [
    "#空值太多  可以先判断一下 是否是空值 然后再往里面添加\n",
    "#遍历一下dataframe中的数据 \n",
    "for i in range(0,dataset.shape[0]):#遍历所有的行\n",
    "    temp=[]\n",
    "    for j in range(0,dataset.shape[1]): #在每一行中去遍历每一个列元素，dataset.shape[0]=7501 dataset.shape[1]=20\n",
    "        #把每一个元素转成一个字符串str 并添加到temp列表中\n",
    "        #如果这个字符串是nan那就不添加了  \n",
    "        if str(dataset.values[i][j])!='nan':\n",
    "            temp.append(str(dataset.values[i,j]))#这里range的范围分别取到7500和19 而不是7501和20 表格中最大的行列索引也刚好是7500与19\n",
    "    #这个temp会保存每一行的购物篮的商品的名称的str类型 不过进行下一个for循环会被重新赋值为0 重新存\n",
    "    #然后每一次循环中的temp放到外面的大的trasactions[]中\n",
    "    transactions.append(temp)\n",
    "print(transactions[0:10])#这个时候就是一个大的列表trasactions中 放了很多个小的列表temp temp中的元素是购物的商品的字符串形式    \n",
    "#print前10个吧 否则上传到github之后 全部显示出来 导致网页太长 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## transactions构建好了 来用apriori挖掘规则"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "频繁项集：\n",
      " {1: {('olive oil',): 494, ('frozen smoothie',): 475, ('tomato juice',): 228, ('vegetables mix',): 193, ('low fat yogurt',): 574, ('honey',): 356, ('salmon',): 319, ('cottage cheese',): 239, ('avocado',): 250, ('almonds',): 153, ('shrimp',): 536, ('mineral water',): 1788, ('energy drink',): 200, ('green tea',): 991, ('burgers',): 654, ('meatballs',): 157, ('eggs',): 1348, ('turkey',): 469, ('milk',): 972, ('whole wheat rice',): 439, ('energy bar',): 203, ('whole wheat pasta',): 221, ('french fries',): 1282, ('soup',): 379, ('spaghetti',): 1306, ('frozen vegetables',): 715, ('cookies',): 603, ('cooking oil',): 383, ('champagne',): 351, ('chocolate',): 1229, ('chicken',): 450, ('oil',): 173, ('fresh tuna',): 167, ('tomatoes',): 513, ('red wine',): 211, ('pepper',): 199, ('ham',): 199, ('pancakes',): 713, ('grated cheese',): 393, ('fresh bread',): 323, ('escalope',): 595, ('ground beef',): 737, ('herb & pepper',): 371, ('strawberries',): 160, ('cake',): 608, ('hot dogs',): 243, ('brownies',): 253, ('cereals',): 193, ('muffins',): 181, ('light mayo',): 204, ('yogurt cake',): 205, ('butter',): 226, ('french wine',): 169}, 2: {('frozen smoothie', 'mineral water'): 152, ('green tea', 'mineral water'): 233, ('low fat yogurt', 'mineral water'): 180, ('mineral water', 'olive oil'): 207, ('mineral water', 'shrimp'): 177, ('burgers', 'eggs'): 216, ('milk', 'mineral water'): 360, ('mineral water', 'whole wheat rice'): 151, ('frozen vegetables', 'spaghetti'): 209, ('green tea', 'spaghetti'): 199, ('burgers', 'mineral water'): 183, ('cooking oil', 'mineral water'): 151, ('eggs', 'mineral water'): 382, ('chicken', 'mineral water'): 171, ('eggs', 'spaghetti'): 274, ('mineral water', 'spaghetti'): 448, ('mineral water', 'tomatoes'): 183, ('spaghetti', 'tomatoes'): 157, ('french fries', 'milk'): 178, ('chocolate', 'eggs'): 249, ('mineral water', 'pancakes'): 253, ('pancakes', 'spaghetti'): 189, ('milk', 'spaghetti'): 266, ('ground beef', 'milk'): 165, ('ground beef', 'mineral water'): 307, ('ground beef', 'spaghetti'): 294, ('chocolate', 'french fries'): 258, ('chocolate', 'mineral water'): 395, ('eggs', 'french fries'): 273, ('french fries', 'mineral water'): 253, ('frozen vegetables', 'mineral water'): 268, ('chocolate', 'frozen vegetables'): 172, ('cake', 'mineral water'): 206, ('french fries', 'green tea'): 214, ('french fries', 'pancakes'): 151, ('chocolate', 'green tea'): 176, ('chocolate', 'spaghetti'): 294, ('chocolate', 'ground beef'): 173, ('chocolate', 'milk'): 241, ('frozen vegetables', 'milk'): 177, ('mineral water', 'soup'): 173, ('olive oil', 'spaghetti'): 172, ('burgers', 'french fries'): 165, ('burgers', 'spaghetti'): 161, ('french fries', 'spaghetti'): 207, ('shrimp', 'spaghetti'): 159, ('eggs', 'green tea'): 191, ('eggs', 'milk'): 231, ('eggs', 'pancakes'): 163, ('eggs', 'frozen vegetables'): 163}}\n",
      "关联规则：\n",
      " [{frozen smoothie} -> {mineral water}, {low fat yogurt} -> {mineral water}, {olive oil} -> {mineral water}, {shrimp} -> {mineral water}, {burgers} -> {eggs}, {milk} -> {mineral water}, {whole wheat rice} -> {mineral water}, {cooking oil} -> {mineral water}, {chicken} -> {mineral water}, {spaghetti} -> {mineral water}, {tomatoes} -> {mineral water}, {tomatoes} -> {spaghetti}, {pancakes} -> {mineral water}, {ground beef} -> {mineral water}, {ground beef} -> {spaghetti}, {chocolate} -> {mineral water}, {frozen vegetables} -> {mineral water}, {cake} -> {mineral water}, {soup} -> {mineral water}, {olive oil} -> {spaghetti}]\n"
     ]
    }
   ],
   "source": [
    "#然后把这个trasactions放入到apriori中去 去跑一下频繁项集和关联规则\n",
    "#包已经在最上面导入了 \n",
    "# 挖掘频繁项集和频繁规则\n",
    "itemsets, rules = apriori(transactions, min_support=0.02,  min_confidence=0.3)\n",
    "print('频繁项集：\\n', itemsets)#大的字典只有两个 12  大于最小支持度为0.02就是这些商品组合   没有三个商品组合还支持度大于0.02的\n",
    "print('关联规则：\\n', rules)#在最小支持度为0.02和最小置信度大于0.3的就是这些组合  {}->{}前面括号对括号的置信度"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
